var imgFile;
$(function () {
    getArticle();
});
/*重置*/
function resetClick(){
    $(".dis-inebl input").val("");
    $("#userState option:selected").text("全部");
    getUserList();
}
/*查询文章*/
function getArticle(){
    JY.Model.loading();
    $.ajax({
        url : jypath + "/article/getYmArticlePagination",
        type : "get",
        data:{
            "articleType":4
        },
        jsonType : "json",
        success : function(data) {
            var html="";
            if(data.obj.length>0&&data.obj!=null){
               for(var i=0;i<data.obj.length;i++){
                   var l=data.obj[i];
                   html+="<option value="+l.articleId+">"+l.articleTitle+"</option>"
               }
               $("#articleTitle").append(html)
                JY.Model.loadingClose();
            }
        }
    })
    getUserList();
}
/*查询*/
function getUserList(init){
    JY.Model.loading();
    var pageNum=$("#baseForm .pageNum").val();
    var pageSize=$("#baseForm .pageSize").val();
	$("#ticktTable tbody").html("");
	var userState=$("#userState option:selected").val();
    if($("#userState option:selected").text()=="全部"){
        userState="";
    }
	$.ajax({
		url : jypath + "/article/getScrollArticleByParameter",
		type : "get",
		data:{
            "pageNum":pageNum,
            "pageSize":pageSize,
            "state":userState,
		},
		jsonType : "json",
		success : function(data) {
            var data=data instanceof Object ? data : JSON.parse(data);
            var html="";
            var list=data.obj;
            var results=list.results;
            var pageNum=list.pageNum,
                pageSize=list.pageSize,
                totalRecord=list.totalRecord;
           if(results!=null&&results.length>0){
                var leng=(pageNum-1)*pageSize;//计算序号
                for(var i = 0;i<results.length;i++){
                    var l=results[i];
                    html+="<tr class='tr_color'>";
                    html+="<td class='center ver-m'><label><input type='checkbox' name='ids' value='"+l.scrollArticleId+"' class='ace' /> <span class='lbl'></span></label></td>";
                    html+="<td class='center'><img src="+l.attachmentUrl+" style='width:100px;height:50px;' /></td>";
                    html+="<td class='center'>"+l.articleTitle+"</td>";

                    if(l.scrollType==1){
                        html+="<td class='center'>首页</td>";
                    }else if(l.scrollType==2){
                        html+="<td class='center'>其他</td>";
                    }
                    if(l.state==1){
                        html+="<td class='center'>有效</td>";
                    }else if(l.state==0){
                        html+="<td class='center'>无效</td>";
                    }
                    html+="</tr>";
                }
                $("#ticktTable tbody").append(html);
                JY.Page.setPage("baseForm","pageing",pageSize,pageNum,totalRecord,"getUserList");
            }else{
                html+="<tr><td colspan='5' class='center'>没有相关数据</td></tr>";
                $("#ticktTable tbody").append(html);
                $("#baseForm ul").empty();//清空分页
            }
            JY.Model.loadingClose();
		}
	})
}
//图片预览
function preview(obj) {
    var url= window.URL.createObjectURL(obj.files[0]);
    $(obj).prev().attr("src",url);
    imgFile=obj.files[0];
}
//新增
function addAri(){
    clearForm();
    JY.Model.edit("auForm_edit","新增轮播图",function(){
        var that=$(this);
        var articleTitle=$("#articleTitle").find("option:selected").val();
        if(articleTitle==""||articleTitle==undefined){
            JY.Model.info("请先新增文章!");
            that.dialog("close");
        }else if($(".preview_ing").attr("src")==""+jypath+"/static/images/back/add_img.jpg"||imgFile.name==undefined){
            JY.Model.info("请先上传轮播图!");
        }else{
            $(".masks").removeClass("hide");
            $.ajax({
                url : jypath + "/article/insertScrollArticle",
                type : "post",
                data:{
                    "scrollType":$("#menuName").find("option:selected").val(),
                    "articleId":$("#articleTitle").find("option:selected").val(),
                },
                jsonType : "json",
                success : function(data) {
                    if(data.res==1){
                        var relevanceId=data.obj;
                        var formData=new FormData();
                        formData.append("imageFile",imgFile);
                        formData.append("relevanceId",relevanceId);
                        formData.append("relevanceTpyeId",8);
                        formData.append("relevanceTpyeDetail",0);
                        $.ajax({
                            url: jypath + "/uploadVideoAndImage",
                            type: "post",
                            processData: false,
                            contentType: false,
                            data: formData,
                            success: function (data) {
                                that.dialog("close");
                                $(".masks").addClass("hide");
                                JY.Model.info("操作成功!",function(){getUserList();});
                            }
                        })

                    }else{
                        that.dialog("close");
                    }
                }
            })
        }
    })
}
// 启用禁用
function enAbleClick(type){
    var chks =[];
    $('#ticktTable input[name="ids"]:checked').each(function(){
        chks.push($(this).val());
    });
    if(chks.length==0) {
        JY.Model.info("您没有选择任何内容!");
    }else if(chks.length>0){
        var states="";
        if(type==1){
            //启用
            states=1;
        }else if(type==0){
            //禁用
            states=0;
        }
        JY.Model.message("auForm_edits","修改状态",function(){
            var that = $(this);
            $.ajax({
                url : jypath + "/article/updateScollArticleState",
                type : "POST",
                data:{
                    "state":states,
                    "scrollArticleId":chks.join()
                },
                jsonType : "json",
                success : function(data) {
                   if(data.res==1){
                       that.dialog("close");
                       JY.Model.info("操作成功!",function(){getUserList();});
                   }
                }
            })
        });
    }
}
function clearForm(){
    imgFile={};
    $(".preview_ing").attr("src",""+jypath+"/static/images/back/add_img.jpg");
    $("#input1").val("");
}